<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="79px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker clearable
              v-model="queryParams.createTime"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择创建时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="loading" :data="MessageList" @selection-change="handleSelectionChange">
      <el-table-column label="标题" align="left" prop="title" min-width="150" show-overflow-tooltip="true"/>
      <el-table-column label="内容" align="left" prop="content" min-width="200" show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="left" prop="status" >
        <template #default="scope">
          {{ scope.row.status==0 ? '未读' : '已读' }}
        </template>
      </el-table-column>
      <!--<el-table-column label="接收人名称" align="left" prop="userName" />
      <el-table-column label="关联来源id" align="center" prop="sourceId">
        <template #default="scope">
          {{ scope.row.sourceId!=null && scope.row.sourceId!='null'? scope.row.sourceId : '' }}
        </template>
      </el-table-column>
      <el-table-column label="创建者名称" align="left" prop="sourceName" />-->
      <el-table-column label="创建时间" align="left" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">查看</el-button>
        </template>
      </el-table-column>-->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改消息对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body style="margin-top: 99px;">
      <el-form ref="MessageRef" :model="form" :rules="rules" label-width="109px" :disabled="true">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title"/>
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :height="199"/>
        </el-form-item>
        <el-form-item label="接收人名称" prop="userName">
          <el-input v-model="form.userName"/>
        </el-form-item>
        <el-form-item label="创建者名称" prop="sourceName">
          <el-input v-model="form.sourceName"/>
        </el-form-item>
        <el-form-item label="关联来源id" prop="sourceId">
          <el-input v-model="form.sourceId"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Message">
import { listMessage, getMessage, delMessage, addMessage, updateMessage } from "@/api/partybuilding/Message";

const { proxy } = getCurrentInstance();

const MessageList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: null,
    title: null,
    type: null,
    content: null,
    url: null,
    status: null,
    endTime: null,
    userId: null,
    userName: null,
    sourceName: null,
    sourceId: null,
    createTime: null
  },
  rules: {
    title: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);


// 获取参数值
function getQueryString(name) {
  // 获取当前页面的 URL
  const currentUrl = window.location.href;
  // 创建 URL 对象
  const url = new URL(currentUrl);
  // 获取名为 "name" 的参数值
  const nameParam = url.searchParams.get(name);
  // 输出获取到的参数值
  //console.log("signLogin=", nameParam);
  return nameParam;
}
onMounted(() => {
  let idVal = getQueryString("id");
  console.log("接收消息中......idVal=", idVal);
  if (idVal!=null) {
    queryParams.value.id = idVal;
  }
  getList();
});

/** 查询消息列表 */
function getList() {
  loading.value = true;
  listMessage(queryParams.value).then(response => {
    MessageList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    type: null,
    content: null,
    url: null,
    status: null,
    endTime: null,
    userId: null,
    userName: null,
    sourceName: null,
    sourceId: null,
    remark: null,
    createBy: null,
    createTime: null
  };
  proxy.resetForm("MessageRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.id = null;
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getMessage(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改消息";
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('partybuilding/Message/export', {
    ...queryParams.value
  }, `Message_${new Date().getTime()}.xlsx`)
}
</script>
